package com.fzz.tools;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Utils {

    public List<String> getVideoUrl(String url) throws FailingHttpStatusCodeException, MalformedURLException, IOException {

        // TODO Auto-generated method stub


        WebClient wc = new WebClient(BrowserVersion.CHROME);

        wc.setJavaScriptTimeout(5000);

        wc.getOptions().setUseInsecureSSL(true);//接受任何主机连接 无论是否有有效证书

        wc.getOptions().setJavaScriptEnabled(true);//设置支持javascript脚本

        wc.getOptions().setCssEnabled(false);//禁用css支持

        wc.getOptions().setThrowExceptionOnScriptError(false);//js运行错误时不抛出异常

        wc.getOptions().setTimeout(100000);//设置连接超时时间

        wc.getOptions().setDoNotTrackEnabled(false);

        HtmlPage page = wc.getPage(url);


        String pageXml = page.asXml();//直接将加载完成的页面转换成xml格式的字符串

        //System.out.println(pageXml);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //Connection conn = Jsoup.connect(pageXml); // 建立与url中页面的连接
        //Document doc = conn.get(); // 解析页面
        Document doc = Jsoup.parse(pageXml);
        Elements links = doc.select("iframe[src]"); // 获取页面中所有的超链接
        Elements elements = links.select("[src]");
        List<String> list=new ArrayList<String>();
        for (Element element : elements) {
            String href = element.attr("src");
            /*if (href.contains(".mp4")) {
                    String videoUrl=href.substring(href.indexOf("http"),href.indexOf(".mp4"));
                System.out.println("=============================="+videoUrl);
                videoUrl+=".mp4";
                list.add(href);
                List<String> nameAndImg = getNameAndImg(url);
                for (String s : nameAndImg) {
                    list.add(s);
                }
            }*/

            list.add(href);
            List<String> nameAndImg = getNameAndImg(url);
            for (String s : nameAndImg) {
                list.add(s);
            }
        }

        return list;
    }

    public List<String> getViewUrl(String url) throws FailingHttpStatusCodeException, MalformedURLException, IOException {

        // TODO Auto-generated method stub

        List<String> list=new ArrayList<String>();
        WebClient wc = new WebClient(BrowserVersion.CHROME);

        wc.setJavaScriptTimeout(5000);

        wc.getOptions().setUseInsecureSSL(true);//接受任何主机连接 无论是否有有效证书

        wc.getOptions().setJavaScriptEnabled(true);//设置支持javascript脚本

        wc.getOptions().setCssEnabled(false);//禁用css支持

        wc.getOptions().setThrowExceptionOnScriptError(false);//js运行错误时不抛出异常

        wc.getOptions().setTimeout(100000);//设置连接超时时间

        wc.getOptions().setDoNotTrackEnabled(false);

        HtmlPage page = wc.getPage(url);


        String pageXml = page.asXml();//直接将加载完成的页面转换成xml格式的字符串

        //System.out.println(pageXml);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //Connection conn = Jsoup.connect(pageXml); // 建立与url中页面的连接
        //Document doc = conn.get(); // 解析页面
        Document doc = Jsoup.parse(pageXml);
        Elements links = doc.select("a[href]"); // 获取页面中所有的超链接
        Elements elements = links.select("[href]");
        for (Element element : elements) {
            String href = element.attr("href");
            if(href.startsWith("/view/")) {
                System.out.println("=============================="+href);
                list.add(href);
            }
        }
        return list;
        /*for (Element element : elements) {
            String href = element.attr("href");
            if(href.startsWith("/view/")) {
                getPlayUrl("http://www.imomoe.ai"+href);
            }
        }*/

    }

    public List<String> getPlayUrl(String url) throws FailingHttpStatusCodeException, MalformedURLException, IOException {

        // TODO Auto-generated method stub

        List<String> list=new ArrayList<String>();

        WebClient wc = new WebClient(BrowserVersion.CHROME);

        wc.setJavaScriptTimeout(5000);

        wc.getOptions().setUseInsecureSSL(true);//接受任何主机连接 无论是否有有效证书

        wc.getOptions().setJavaScriptEnabled(true);//设置支持javascript脚本

        wc.getOptions().setCssEnabled(false);//禁用css支持

        wc.getOptions().setThrowExceptionOnScriptError(false);//js运行错误时不抛出异常

        wc.getOptions().setTimeout(100000);//设置连接超时时间

        wc.getOptions().setDoNotTrackEnabled(false);

        HtmlPage page = wc.getPage(url);


        String pageXml = page.asXml();//直接将加载完成的页面转换成xml格式的字符串

        //System.out.println(pageXml);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //Connection conn = Jsoup.connect(pageXml); // 建立与url中页面的连接
        //Document doc = conn.get(); // 解析页面
        Document doc = Jsoup.parse(pageXml);
        Elements links = doc.select("a[href]"); // 获取页面中所有的超链接
        Elements elements = links.select("[href]");
        for (Element element : elements) {
            String href = element.attr("href");
            if(href.startsWith("/player/")) {
                System.out.println("=============================="+href);
                list.add(href);
            }
        }
        /*for (Element element : elements) {
            String href = element.attr("href");
            if(href.startsWith("/player/")) {
                getVideoUrl("http://www.imomoe.ai"+href);
            }
        }*/

        return list;
    }


    public List<String> getNameAndImg(String url) throws FailingHttpStatusCodeException, MalformedURLException, IOException {

        // TODO Auto-generated method stub

        List<String> list=new ArrayList<String>();
        String replace = url.replace("player", "view");
        String newUrl=replace.substring(replace.indexOf("http"),replace.indexOf("-"));
        newUrl+=".html";
        WebClient wc = new WebClient(BrowserVersion.CHROME);

        wc.setJavaScriptTimeout(5000);

        wc.getOptions().setUseInsecureSSL(true);//接受任何主机连接 无论是否有有效证书

        wc.getOptions().setJavaScriptEnabled(true);//设置支持javascript脚本

        wc.getOptions().setCssEnabled(false);//禁用css支持

        wc.getOptions().setThrowExceptionOnScriptError(false);//js运行错误时不抛出异常

        wc.getOptions().setTimeout(100000);//设置连接超时时间

        wc.getOptions().setDoNotTrackEnabled(false);

        HtmlPage page = wc.getPage(newUrl);


        String pageXml = page.asXml();//直接将加载完成的页面转换成xml格式的字符串

        //System.out.println(pageXml);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //Connection conn = Jsoup.connect(pageXml); // 建立与url中页面的连接
        //Document doc = conn.get(); // 解析页面
        Document doc = Jsoup.parse(pageXml);
        Elements links = doc.select("img[src]"); // 获取页面中所有的超链接
        Elements elements = links.select("[src]");
        boolean flag=true;
        for (Element element : elements) {
            String src = element.attr("src");
            if(src.startsWith("http")) {
                System.out.println("=============================="+src);
                if(flag)
                {
                    list.add(src);
                }
            }
            if(flag)
            {
                System.out.println(element.attr("alt"));
                list.add(element.attr("alt"));
                flag=false;
            }
        }
        //return list;
        return list;
    }
}


